package com.mall.user.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;

/**
 * @author: lds
 * @clsss: EmailUtil
 */
@Component
public class EmailUtil {

    @Autowired
    JavaMailSender mailSender;

    public  String verificationCode(String email){
        System.err.println(1111);
        String s = RandomUtil.randomString();

        String emailServiceCode=Md5Util.getMd5(s);
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("后台管理系统发来的验证码");
        simpleMailMessage.setText("验证码为："+emailServiceCode);
        simpleMailMessage.setFrom("1608394168@qq.com");

        simpleMailMessage.setTo(email);
        mailSender.send(simpleMailMessage);
        return emailServiceCode;
    }



    public  void changeCode(String email,String token){

        String emailServiceCode="1234";
        MimeMessage message=mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom("1608394168@qq.com");
            helper.setTo(email);
            helper.setSubject("登录IP异常");
            helper.setText("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
                    "<html>\n" +
                    "<head>\n" +
                    "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" +
                    "    <title>Mall Account Login Exception</title>\n" +
                    "    <style media=\"all\" type=\"text/css\">\n" +
                    "        td, p, h1, h3, a {\n" +
                    "            font-family: Helvetica, Arial, sans-serif;\n" +
                    "        }\n" +
                    "        a\n" +
                    "        {\n" +
                    "            color: #66c0f4;\n" +
                    "            text-decoration: none;\n" +
                    "        }\n" +
                    "        a:hover\n" +
                    "        {\n" +
                    "            color: #66c0f4;\n" +
                    "            text-decoration: none;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body bgcolor=\"\" LINK=\"#6d93b8\" ALINK=\"#9DB7D0\" VLINK=\"#6d93b8\" TEXT=\"#d7d7d7\" style=\"font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #d7d7d7;\">\n" +
                    "\n" +
                    "<table style=\"width: 538px; background-color: #393836;\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                    "    <tr>\n" +
                    "        <td style=\" height: 65px; background-color: #171a21; border-bottom: 1px solid #4d4b48;\">\n" +
                    "            <!--<img src=\"http://store.edgecast.steamstatic.com/public/images/email/email_header_logo.png?v=1\" width=\"538\" height=\"65\" alt=\"Mall\">-->\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "    <tr>\n" +
                    "        <td bgcolor=\"#17212e\">\n" +
                    "            <table width=\"470\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding-left: 5px; padding-right: 5px; padding-bottom: 10px;\">\n" +
                    "\n" +
                    "                <tr bgcolor=\"#17212e\">\n" +
                    "                    <td style=\"padding-top: 32px;\">\n" +
                    "\t\t\t\t\t<span style=\"font-size: 24px; color: #66c0f4; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "\t\t\t\t\t\t您的账户正在异常登录\t\t\t\t\t</span><br>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"padding-top: 12px; font-size: 17px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "                        点击下方链接即可强制下线:\t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"padding-top: 24px; padding-bottom: 24px;\">\n" +
                    "                        <div>\n" +
                    "                            <span style=\"font-size: 24px; color: #66c0f4; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "                            \n" +
                    "                                <a target=\"_blank\" href=\"http://localhost:8080/user/verify?uid="+token+"\" style=\"text-decoration:none;\">\n" +
                    "                                    强制下线\n" +
                    "                                </a>\n" +
                    "                            </span>\n" +
                    "                            </br>\n" +
                    "                        </div>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr bgcolor=\"#121a25\">\n" +
                    "                    <td style=\"padding: 20px; font-size: 12px; line-height: 17px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif;\">\n" +
                    "                        \t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"font-size: 12px; color: #6d7880; padding-top: 16px; padding-bottom: 60px;\">\n" +
                    "                        <br>\n" +
                    "                        <a style=\"color: #8f98a0;\" href=\"http://localhost:9527/#/login/\">http://www.market.com/</a><br>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "            </table>\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "\n" +
                    "    <tr style=\"background-color: #000000;\">\n" +
                    "        <td style=\"padding: 12px 24px;\">\n" +
                    "            <table cellpadding=\"0\" cellspacing=\"0\">\n" +
                    "                <tr>\n" +
                    "                    <td width=\"92\">\n" +
                    "                        <!--<img src=\"http://store.edgecast.steamstatic.com/public/images/logo_valve_footer.jpg\" width=\"92\" height=\"26\" alt=\"Mall&reg;\">-->\n" +
                    "                    </td>\n" +
                    "                    <td style=\" font-size: 11px; color: #595959; padding-left: 12px;\">\n" +
                    "                        前端页面实在不会写只能借g胖的页面用一用了<br>\n" +
                    "                       \t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "            </table>\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "</table>\n" +
                    "<center style=\"color: #000000; font-size: 11px; margin-bottom: 4px;\">问题如果还没有解决?  <a target=\"_blank\" href=\"http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=JRQTFR0WHBEUEx1lVFQLRkpI\" style=\"text-decoration:none;\">\n" +
                    "    点这里.<img src=\"http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_22.png\"></a></center>\n" +
                    "\n" +
                    "</body>\n" +
                    "</html>\n",true);
            mailSender.send(message);
            System.out.println("html格式邮件发送成功");
        }catch (Exception e){
            System.out.println("html格式邮件发送失败");}


    }
    public  void changeCode2(String email,String uuid,String username){


        MimeMessage message=mailSender.createMimeMessage();
        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper=new MimeMessageHelper(message,true);
            helper.setFrom("brainlds@163.com");
            helper.setTo(email);
            helper.setSubject("注册激活");
            helper.setText("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
                    "<html>\n" +
                    "<head>\n" +
                    "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n" +
                    "    <title>Mall Account Login Exception</title>\n" +
                    "    <style media=\"all\" type=\"text/css\">\n" +
                    "        td, p, h1, h3, a {\n" +
                    "            font-family: Helvetica, Arial, sans-serif;\n" +
                    "        }\n" +
                    "        a\n" +
                    "        {\n" +
                    "            color: #66c0f4;\n" +
                    "            text-decoration: none;\n" +
                    "        }\n" +
                    "        a:hover\n" +
                    "        {\n" +
                    "            color: #66c0f4;\n" +
                    "            text-decoration: none;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "</head>\n" +
                    "<body bgcolor=\"\" LINK=\"#6d93b8\" ALINK=\"#9DB7D0\" VLINK=\"#6d93b8\" TEXT=\"#d7d7d7\" style=\"font-family: Helvetica, Arial, sans-serif; font-size: 14px; color: #d7d7d7;\">\n" +
                    "\n" +
                    "<table style=\"width: 538px; background-color: #393836;\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n" +
                    "    <tr>\n" +
                    "        <td style=\" height: 65px; background-color: #171a21; border-bottom: 1px solid #4d4b48;\">\n" +
                    "            <!--<img src=\"http://store.edgecast.steamstatic.com/public/images/email/email_header_logo.png?v=1\" width=\"538\" height=\"65\" alt=\"Mall\">-->\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "    <tr>\n" +
                    "        <td bgcolor=\"#17212e\">\n" +
                    "            <table width=\"470\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"padding-left: 5px; padding-right: 5px; padding-bottom: 10px;\">\n" +
                    "\n" +
                    "                <tr bgcolor=\"#17212e\">\n" +
                    "                    <td style=\"padding-top: 32px;\">\n" +
                    "\t\t\t\t\t<span style=\"font-size: 24px; color: #66c0f4; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "\t\t\t\t\t\t多来买用户激活\t\t\t\t\t</span><br>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"padding-top: 12px; font-size: 17px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "                        点击下方链接即可激活用户:\t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"padding-top: 24px; padding-bottom: 24px;\">\n" +
                    "                        <div>\n" +
                    "                            <span style=\"font-size: 24px; color: #66c0f4; font-family: Arial, Helvetica, sans-serif; font-weight: bold;\">\n" +
                    "                            \n" +
                    "                                <a target=\"_blank\" href=\"http://localhost:8080/user/verify?uid="+uuid+"&username="+username+"\" style=\"text-decoration:none;\">\n" +
                    "                                    激活链接\n" +
                    "                                </a>\n" +
                    "                            </span>\n" +
                    "                            </br>\n" +
                    "                        </div>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr bgcolor=\"#121a25\">\n" +
                    "                    <td style=\"padding: 20px; font-size: 12px; line-height: 17px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif;\">\n" +
                    "                        \t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "                <tr>\n" +
                    "                    <td style=\"font-size: 12px; color: #6d7880; padding-top: 16px; padding-bottom: 60px;\">\n" +
                    "                        <br>\n" +
                    "                        <a style=\"color: #8f98a0;\" href=\"http://localhost:9527/#/login/\">http://www.market.com/</a><br>\n" +
                    "                    </td>\n" +
                    "                </tr>\n" +
                    "\n" +
                    "            </table>\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "\n" +
                    "    <tr style=\"background-color: #000000;\">\n" +
                    "        <td style=\"padding: 12px 24px;\">\n" +
                    "            <table cellpadding=\"0\" cellspacing=\"0\">\n" +
                    "                <tr>\n" +
                    "                    <td width=\"92\">\n" +
                    "                        <!--<img src=\"http://store.edgecast.steamstatic.com/public/images/logo_valve_footer.jpg\" width=\"92\" height=\"26\" alt=\"Mall&reg;\">-->\n" +
                    "                    </td>\n" +
                    "                    <td style=\" font-size: 11px; color: #595959; padding-left: 12px;\">\n" +
                    "                        前端页面实在不会写只能借g胖的页面用一用了<br>\n" +
                    "                       \t\t\t\t\t</td>\n" +
                    "                </tr>\n" +
                    "            </table>\n" +
                    "        </td>\n" +
                    "    </tr>\n" +
                    "</table>\n" +
                    "<center style=\"color: #000000; font-size: 11px; margin-bottom: 4px;\">问题如果还没有解决?  <a target=\"_blank\" href=\"http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=JRQTFR0WHBEUEx1lVFQLRkpI\" style=\"text-decoration:none;\">\n" +
                    "    点这里.<img src=\"http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_22.png\"></a></center>\n" +
                    "\n" +
                    "</body>\n" +
                    "</html>\n",true);
            mailSender.send(message);
            System.out.println("html格式邮件发送成功");
        }catch (Exception e){
            System.out.println("html格式邮件发送失败");}


    }
}
